using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NBear.Common;
using NBear.Data;

namespace NBear.Test.UnitTests
{
    public interface TempTable : IEntity
    {
        [PrimaryKey]
        decimal ID { get; set; }
        [PrimaryKey]
        string ColName { get; set; }
    }

    [Table("TempTable")]
    public interface TempTable2 : IEntity
    {
        [PrimaryKey]
        decimal ID { get; set; }
        string ColName { get; set; }
    }

    [Table("select \"t1\".\"ID\", \"t1\".\"ColName\" from \"TempTable\" \"t1\" inner join \"TempTable\" \"t2\" on \"t1\".\"ID\" = \"t2\".\"ID\"", IsView = true)]
    public interface TempTableJoin : IEntity
    {
        [PrimaryKey]
        decimal ID { get; set; }
        string ColName { get; set; }
    }

    /// <interface>
    /// Summary description for TestOracleDbProvider
    /// </summary>
    [TestClass]
    public class TestOracleDbProvider
    {
        public TestOracleDbProvider()
        {
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) 
        {
            Gateway.Default = new Gateway(DatabaseType.Oracle,
                "Data Source=localhost;User ID=system;Password=sa;Unicode=True");
        }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        //[TestMethod]
        public void TestOracleDbProviderGateway()
        {
            TempTable[] objs = Gateway.Default.SelectAll<TempTable>();

            TempTableJoin[] objs3 = Gateway.Default.SelectAll<TempTableJoin>();
            objs3 = Gateway.Default.SelectAll<TempTableJoin>(2, 2);

            TempTable2[] obj2s = Gateway.Default.Select<TempTable2>(5, 2, "ID < :ID", "ID desc", new object[] { 5 });

            TempTable2 obj = EntityFactory<TempTable2>.CreateObject();
            obj.ColName = Guid.NewGuid().ToString();
            obj.ID = 1;

            System.Data.Common.DbTransaction tran = Gateway.Default.BeginTransaction();

            Gateway.Default.Insert<TempTable2>(obj, tran);

            obj.ColName = Guid.NewGuid().ToString();
            Gateway.Default.Update<TempTable2>(obj, tran);

            Gateway.Default.Delete<TempTable2>("ID = :ID", new object[] { 1 }, tran);

            tran.Commit();
        }
    }
}
